package com.gxa.modules.communityManagement.controller;

import cn.hutool.core.codec.Base64;
import com.gxa.common.utils.*;

import com.gxa.modules.communityManagement.entity.HouseholdManagement;

import com.gxa.modules.communityManagement.form.HouseholdManagementForm;

import com.gxa.modules.communityManagement.service.HouseholdManagementService;

import com.gxa.modules.sys.redis.SysUserRedis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Api(tags = "住户管理接口")
@RestController
@Slf4j
public class HouseholdManagementController {
    @Autowired
    private HouseholdManagementService householdManagementService;
    @Autowired
    private SysUserRedis sysUserRedis;

    @ApiOperation(value="住户管理查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value ="当前是第几页",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value ="每页显示多少条",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "communityId",value ="查询条件-小区ID",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "type",value ="查询条件-住户类型",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "name",value ="查询条件-住户名字",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "startTime",value ="查询条件-提交审核开始时间",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "stopTime",value ="查询条件-提交审核结束时间",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "ptime",value ="查询条件-日期",dataType ="String"),
    }
    )

    @GetMapping("/householdManagement/list")
    public Result<PageUtils> list(@RequestParam Map<String,Object> params){
        log.info("----params-----{}----",params);
        PageUtils pageUtils = this.householdManagementService.queryByPage(params);
        return new Result<PageUtils>().ok(pageUtils);
    }

    @ApiOperation(value="住户管理添加接口")
    @PostMapping("/householdManagement/add")
    public Result add(@RequestBody HouseholdManagementForm householdManagementForm){
        log.info("----body-----{}----",householdManagementForm);
        Map<String, Object> params=new HashMap<>();
//        判断住户编号是否重复
        params.put("serialNo",householdManagementForm.getSerialNo());
        PageUtils pageUtils = this.householdManagementService.queryByPage(params);
        if (pageUtils.getTotalCount()!=0){
            return new Result().error(1,"住户编号重复");
        }
        HouseholdManagement householdManagement=new HouseholdManagement(householdManagementForm);
        householdManagement.setState("审核中");
        this.householdManagementService.save(householdManagement);
        return new Result().ok();
    }

    @ApiOperation(value="住户管理删除接口")
    @DeleteMapping ("/householdManagement/delete")
    public Result delete(@RequestBody List<Integer> ids){
        log.info("----param-----{}----",ids);
        this.householdManagementService.removeByIds(ids);
        return new Result().ok();
    }

    @ApiOperation(value="住户管理修改接口")
    @PutMapping ("/householdManagement/update")
    public Result update(@RequestBody HouseholdManagementForm householdManagementForm){
        log.info("----param-----{}----",householdManagementForm);
        HouseholdManagement householdManagement=new HouseholdManagement(householdManagementForm);
        this.householdManagementService.updateById(householdManagement);
        return new Result().ok();
    }

    @ApiOperation(value="小程序登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "captcha",value ="验证码",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "uuid",value ="UUID",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "phoneNumber",value ="手机号",dataType ="String"),
    }
    )
    @GetMapping("/householdManagement/login")
    public Result login(@RequestParam  Map<String,Object> params){

        log.info("user------->{}",params);

        String captcha = (String) params.get("captcha");
        String uuid = (String) params.get("uuid");
        String redisGetCaptcha = this.sysUserRedis.getCode(uuid);
        log.info("user------->{}",redisGetCaptcha);

        if (!redisGetCaptcha.equals(captcha)){
            return new Result().error(ErrorCode.ACCOUNT_PASSWORD_ERROR,"验证码不正确或者已经失效");
        }

        //1、拿着用户名去 查询用户信息
        PageUtils householdManagement = this.householdManagementService.queryByPage(params);
        if (householdManagement.getTotalCount()!=0){
            return new Result().ok(householdManagement);
        }
        return new Result().error(1);
    }



}
